package com.ca.ridersclubapplication.view.list;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ridersclubapplication.data.entity.ClubClassify;
import com.ca.ridersclubapplication.view.bar.SortListBar;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.adapter.SortClubAdapter;
import com.ca.ridersclubapplication.util.PinYinUtils;

import java.util.List;

public class SortListView extends ConstraintLayout {

    private Activity activity;

    private List<ClubClassify> list;

    public RecyclerView rv;
    private SortClubAdapter adapter;

    public SortListBar bar;

    @RequiresApi(api = Build.VERSION_CODES.M)
    public SortListView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public SortListView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public SortListView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.sort_list_layout, this);

        rv = view.findViewById(R.id.rv);
        bar = view.findViewById(R.id.bar);

        //  创建Adapter
        adapter = new SortClubAdapter(bar, list);

        //  设置RV
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.setAdapter(adapter);

        //  设置RV滑动事件监听，与Bar绑定
        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private int y = 0;

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                y += dy;
                rv.post(() -> {
                    int vh = rv.computeVerticalScrollRange();
                    int count = adapter.getList().size();
                    int index = (int) (1.0 * y / vh * count);
                    if (index >= adapter.getList().size()) {
                        return;
                    }
                    int currentIndex = PinYinUtils.getPinyinInitials(adapter.getList().get(index).getClassify()).charAt(0) - 'A';
                    bar.setCurrentIndex(currentIndex);
                });
            }
        });

        //  为Bar设置RV，与RV 绑定
        bar.setRv(rv);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void setList(List<ClubClassify> list) {
        this.list = list;
        adapter.setList(this.list);
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
        adapter.setActivity(activity);
    }
}
